﻿using MaterialWinforms.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MaterialWinforms.Controls
{
    public class MaterialBaseTextBox : TextBox
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

        private const int EM_SETCUEBANNER = 0x1501;
        private const char EmptyChar = (char)0;
        private const char VisualStylePasswordChar = '\u25CF';
        private const char NonVisualStylePasswordChar = '\u002A';

        private string _hint = string.Empty;
        public string Hint
        {
            get { return _hint; }
            set
            {
                _hint = value;
                SendMessage(Handle, EM_SETCUEBANNER, (int)IntPtr.Zero, Hint);
            }
        }

        private char _passwordChar = EmptyChar;
        public new char PasswordChar
        {
            get { return _passwordChar; }
            set
            {
                _passwordChar = value;
                SetBasePasswordChar();
            }
        }

        public new void SelectAll()
        {
            BeginInvoke((MethodInvoker)delegate ()
            {
                base.Focus();
                base.SelectAll();
            });
        }

        public new void Focus()
        {
            base.Focus();
        }

        private char _useSystemPasswordChar = EmptyChar;
        public new bool UseSystemPasswordChar
        {
            get { return _useSystemPasswordChar != EmptyChar; }
            set
            {
                if (value)
                {
                    _useSystemPasswordChar = Application.RenderWithVisualStyles ? VisualStylePasswordChar : NonVisualStylePasswordChar;
                }
                else
                {
                    _useSystemPasswordChar = EmptyChar;
                }

                SetBasePasswordChar();
            }
        }

        private void SetBasePasswordChar()
        {
            base.PasswordChar = UseSystemPasswordChar ? _useSystemPasswordChar : _passwordChar;
        }

        public MaterialBaseTextBox()
        {
            MaterialContextMenuStrip cms = new TextBoxContextMenuStrip();
            cms.Opening += ContextMenuStripOnOpening;
            cms.OnItemClickStart += ContextMenuStripOnItemClickStart;

            ContextMenuStrip = cms;
            Font = new Font("微软雅黑", 12F);
        }

        private void ContextMenuStripOnItemClickStart(object sender, ToolStripItemClickedEventArgs toolStripItemClickedEventArgs)
        {
            switch (toolStripItemClickedEventArgs.ClickedItem.Text)
            {
                case "Undo":
                    Undo();
                    break;
                case "Cut":
                    Cut();
                    break;
                case "Copy":
                    Copy();
                    break;
                case "Paste":
                    Paste();
                    break;
                case "Delete":
                    SelectedText = string.Empty;
                    break;
                case "Select All":
                    SelectAll();
                    break;
            }
        }

        private void ContextMenuStripOnOpening(object sender, CancelEventArgs cancelEventArgs)
        {
            var strip = sender as TextBoxContextMenuStrip;
            if (strip != null)
            {
                strip.Undo.Enabled = CanUndo;
                strip.Cut.Enabled = !string.IsNullOrEmpty(SelectedText);
                strip.Copy.Enabled = !string.IsNullOrEmpty(SelectedText);
                strip.Paste.Enabled = Clipboard.ContainsText();
                strip.Delete.Enabled = !string.IsNullOrEmpty(SelectedText);
                strip.SelectAll.Enabled = !string.IsNullOrEmpty(Text);
            }
        }
    }

    public class TextBoxContextMenuStrip : MaterialContextMenuStrip
    {
        public readonly ToolStripItem Undo = new MaterialToolStripMenuItem { Text = "Undo" };
        public readonly ToolStripItem Seperator1 = new ToolStripSeparator();
        public readonly ToolStripItem Cut = new MaterialToolStripMenuItem { Text = "Cut" };
        public readonly ToolStripItem Copy = new MaterialToolStripMenuItem { Text = "Copy" };
        public readonly ToolStripItem Paste = new MaterialToolStripMenuItem { Text = "Paste" };
        public readonly ToolStripItem Delete = new MaterialToolStripMenuItem { Text = "Delete" };
        public readonly ToolStripItem Seperator2 = new ToolStripSeparator();
        public readonly ToolStripItem SelectAll = new MaterialToolStripMenuItem { Text = "Select All" };

        public TextBoxContextMenuStrip()
        {
            Items.AddRange(new[]
            {
                    Undo,
                    Seperator1,
                    Cut,
                    Copy,
                    Paste,
                    Delete,
                    Seperator2,
                    SelectAll
                });
        }
    }
}
